package MyFitnessPlate.Handler;

import java.util.ArrayList;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

import MyFitnessPlate.DataSet.ParsedExerciseAddDescriptionDataSet;

public class ExerciseAddDescriptionHandler extends DefaultHandler{

	/*
	 * <xml>
	 * 		 <description>{description}</description> <id>{id}</id> <met>{met}</met>
	 * 		 ...
	 * </xml>
	 */
	// ===========================================================
	// Fields
	// ===========================================================
	@SuppressWarnings("unused")
	private boolean in_xml = false;
	private boolean in_description = false;
	private boolean in_id = false;
	private boolean in_met = false;
	private ArrayList<ParsedExerciseAddDescriptionDataSet> myParsedExerciseAddDescriptionDataSet = null;

	// ===========================================================
	// Getter & Setter
	// ===========================================================
	public ArrayList<ParsedExerciseAddDescriptionDataSet> getParsedData() {
		return this.myParsedExerciseAddDescriptionDataSet;
	}
	// ===========================================================
	// Methods
	// ===========================================================
	
	public void startDocument() throws SAXException {
		this.myParsedExerciseAddDescriptionDataSet = new ArrayList<ParsedExerciseAddDescriptionDataSet>();
	}

	
	public void endDocument() throws SAXException {
		// Nothing to do
	}

	/**
	 * Gets be called on opening tags like: <tag> Can provide attribute(s), when
	 * xml was like: <tag attribute="attributeValue">
	 */
	
	public void startElement(String namespaceURI, String localName, String qName, Attributes atts) throws SAXException {
		if (localName.equals("xml")) {
			this.in_xml = true;
		} else if (localName.equals("description")) {
			this.in_description = true;
			
		} else if (localName.equals("id")) {
			this.in_id = true;
			myParsedExerciseAddDescriptionDataSet.add(new ParsedExerciseAddDescriptionDataSet());
		} else if (localName.equals("met")) {
			this.in_met = true;
		} 
	}

	/**
	 * Gets be called on closing tags like: </tag>
	 */
	
	public void endElement(String namespaceURI, String localName, String qName) throws SAXException {
		if (localName.equals("xml")) {
			this.in_xml = false;
		} else if (localName.equals("description")) {
			this.in_description = false;
		} else if (localName.equals("id")) {
			this.in_id = false;
		} else if (localName.equals("met")) {
			this.in_met = false;
		} 
	}

	/**
	 * Gets be called on the following structure: <tag>characters</tag>
	 */
	@Override
	public void characters(char ch[], int start, int length) {
		String characterString = new String(ch, start, length);		
		if (this.in_id ) {
			myParsedExerciseAddDescriptionDataSet.get(myParsedExerciseAddDescriptionDataSet.size()-1).setId(characterString);
		}if (this.in_description) {
			myParsedExerciseAddDescriptionDataSet.get(myParsedExerciseAddDescriptionDataSet.size()-1).setDescriptionName(characterString);
		}
		if (this.in_met) {
			myParsedExerciseAddDescriptionDataSet.get(myParsedExerciseAddDescriptionDataSet.size()-1).setMet(characterString);
		}	
	}	
}